package cc.telecomdigital.tdstock.trading.Tools;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ListGeneratorHelper
{
	/** Create a Quantitiy Range List by providing a starting point and
	    the count item; when input 10 as the starting point; 10, 20, 30
	    until the count is reached, Support no decimal **/
	public static List<String> CreateQuantityRangeTextList(
			String iStartVolume, int iCount)
	{
		int index;
		List<String> digitList;
		BigDecimal beginValue, offset;

		digitList = new ArrayList<String>();
		if (iCount == 0)
			return digitList;

		beginValue = new BigDecimal(iStartVolume);
		offset = beginValue;
		for (index = 0; index < iCount; index++)
		{
			digitList.add(beginValue.toString());
			beginValue = beginValue.add(offset);
		}
		return digitList;
	}

	/** Create a Prices Range List by providing a Nominal and iStep.
	    iStep is the discrepancy between each.  iTopOffset is the higher
	    limit count to be produced, iBottomOffset is the lower limit count
	    to be produced. If value is defined as abs(0), it will not be placed on the list **/
	public static List<String> CreatePriceRangeList(String iNominal,
			String iStep, int iTopOffset, int iBottomOffset)
	{
		int genCount;
		BigDecimal startValue, testValue;
		BigDecimal nominalValue, stepValue;

		nominalValue = new BigDecimal(iNominal.trim());
		stepValue = new BigDecimal(iStep.trim());

		testValue = stepValue.multiply(new BigDecimal(iTopOffset));
		startValue = nominalValue.subtract(testValue);
		// If it is negative or 0, we do not want that number
		if (startValue.compareTo(new BigDecimal("0")) <= 0)
		{
			genCount = 0;
			if (iStep.compareTo(iNominal) <= 0)
			{
				testValue = nominalValue.divide(stepValue);
				genCount = Double.valueOf(testValue.toString()).intValue();
			}

			if (genCount == 0)
			{
				startValue = nominalValue;
				genCount = 1;
			}
			else
			{
				startValue = nominalValue;
				if (iNominal.compareTo(iStep) >= 0)
					startValue = startValue.subtract(stepValue
							.multiply(new BigDecimal(genCount)));
			}
			// minimum it would generate the BottomOffset;
			genCount += iBottomOffset;
		}
		else
		{
			// suppose we would generate this much
			genCount = iTopOffset + iBottomOffset + 1;
		}
		return CreatePriceRangeTextList(startValue, stepValue, genCount);
	}

	/** Create a Price Range Text List starting from the iStart Value,
	    Step is the discrepany between each.  iGenCount is the number to
	    be created **/
	static List<String> CreatePriceRangeTextList(BigDecimal iStart,
						BigDecimal iStep, int iGenCount)
	{
		int index, scalePrecision;
		List<String> digitList;

		index = iStart.scale();
		scalePrecision = iStep.scale();
		if (index >= scalePrecision)
			scalePrecision = index;

		iStart = iStart.setScale(scalePrecision);
		digitList = new ArrayList<String>();
		for (index = 0; index < iGenCount; index++)
		{
			digitList.add(iStart.toString());
			iStart = iStart.add(iStep);
		}
		return digitList;
	}

	/** With a Text List, inputted and the value to be looked for.  It will tried
	    to look for the closed value within the value list.		
         Suppose the list is in ascending order **/
	public static int GetValueLocation(List<String> iList, String iValue)
	{
		int count, index;
		BigDecimal valueDec;

		count = iList.size();
		// Check if it is greater or less than
		if (count == 0)
			return 0;

		valueDec = new BigDecimal(iValue);
		if (valueDec.compareTo(new BigDecimal(iList.get(0))) == -1)
			return 0;
		if (valueDec.compareTo(new BigDecimal(iList.get(count - 1))) > 0)
			return count - 1;

		for (index = 0; index < count; index++)
		{
			if ((iList.get(index)).equals(iValue))
				return index;
		}
		return -1;
	}

}
